package com.cskaoyan.controller.goods;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.Comment;
import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.goods.base.CommentPageData;
import com.cskaoyan.bean.goods.base.GoodsPageData;
import com.cskaoyan.bean.goods.bo.CommentSearchBO;
import com.cskaoyan.bean.goods.bo.GoodsBo;
import com.cskaoyan.bean.goods.bo.GoodsSearchBO;
import com.cskaoyan.bean.goods.vo.CategoryBrandAndBrandInfoVo;
import com.cskaoyan.bean.goods.vo.GoodsDetailVo;
import com.cskaoyan.service.goods.CommentService;
import com.cskaoyan.service.goods.GoodsService;
import com.cskaoyan.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Description:商品模块的Handler方法
 * @author: Jason 805636901@qq.com
 * @create: 2021-10-18-10:30 PM
 */
@RestController
@RequestMapping("admin")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @GetMapping("goods/list")
    public BaseRespVo list(BaseParam baseParam, GoodsSearchBO goodsSearchBO) {
        GoodsPageData data = goodsService.queryGoods(baseParam, goodsSearchBO);
        return BaseRespVo.ok(data);
    }

    @GetMapping("goods/catAndBrand")
    public BaseRespVo getCategoryAndBrand() {
        CategoryBrandAndBrandInfoVo data = goodsService.getCategoryAndBrand();
        return BaseRespVo.ok(data);
    }
    @PostMapping("goods/delete")
    public BaseRespVo delete(@RequestBody Goods goods) {
        //逻辑删除,根据状态码返回对应的值
        int code = goodsService.deleteGoods(goods);
        if( code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

    @PostMapping("goods/create")
    public BaseRespVo createGoods(@RequestBody GoodsBo goodsInfo) {
        int code = goodsService.createGoods(goodsInfo);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }


    @PostMapping("goods/update")
    public BaseRespVo updateGoods(@RequestBody GoodsBo goodsInfo) {
        int code = goodsService.updateGoods(goodsInfo);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

    @GetMapping("goods/detail")
    public BaseRespVo getGoodsDetail(@NotNull Integer id) {
        GoodsDetailVo detailVo = goodsService.getGoodsDetail(id);
        if (detailVo != null) return BaseRespVo.ok(detailVo);
        return BaseRespVo.error();
    }




    @Autowired
    CommentService commentService;


    @GetMapping("comment/list")
    public BaseRespVo list(BaseParam baseParam, CommentSearchBO commentSearchBO) {
        CommentPageData data = commentService.queryComment(baseParam, commentSearchBO);
        return BaseRespVo.ok(data);
    }

    @PostMapping("comment/delete")
    public BaseRespVo delete(@RequestBody Comment comment) {
        int code = commentService.deleteComment(comment);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

}
